package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.mapper.OrderMapper;
import com.heima.mapper.ReportMapper;
import com.heima.pojo.entity.Order;
import com.heima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询当月订单总数
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer orederCount(LocalDateTime start, LocalDateTime end) {
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.between(true,Order::getCreateTime,start,end);
        List<Order> orders = orderMapper.selectList(qw);
        Integer size = orders.size();
        return size;
    }

    /**
     * 查询当月总销售额
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public BigDecimal orederAmount(LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.between(true,Order::getCreateTime,startTime,endTime);
        List<Order> orders = orderMapper.selectList(qw);
        BigDecimal amount = BigDecimal.ZERO;
        for (Order order : orders) {
            // 将 Integer 转换为 BigDecimal 再相加
            amount = amount.add(BigDecimal.valueOf(order.getAmount()));
        }

        return amount;
    }
}
